using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Tool.Model;
using Tool.Utilty;

namespace Tool.BaseClass
{
	public class GClass53
	{
		public void method_0(GClass19.GClass22 gclass22_0)
		{
			gclass22_0.ReportMessageProgressAction(gclass22_0, "开始准备……", 0, GClass19.GClass25.GEnum1.Begin);
			Thread.Sleep(50);
			Exception ex = null;
			try
			{
				this.method_3(gclass22_0);
			}
			catch (Exception ex2)
			{
				gclass22_0.Collections.CatchError = ex2.ToString();
				ex = ex2;
			}
			foreach (GClass19.GClass22.GClass23 current in gclass22_0._Lists)
			{
				this.method_5(gclass22_0, gclass22_0._rootWebSite + current._FilePath);
			}
			this.method_5(gclass22_0, gclass22_0._rootWebSite + gclass22_0._DirectoryFlash);
			this.method_5(gclass22_0, gclass22_0._rootWebSite + gclass22_0._DirectoryCss);
			this.method_5(gclass22_0, gclass22_0._rootWebSite + gclass22_0._DirectoryJs);
			this.method_5(gclass22_0, gclass22_0._rootWebSite + gclass22_0._DirectoryPicture);
			this.method_5(gclass22_0, gclass22_0._rootWebSite + gclass22_0._DirectoryBackground);
			this.method_5(gclass22_0, gclass22_0._rootWebSite + gclass22_0._DirectoryFonts);
			this.method_2(gclass22_0);
			if (ex != null)
			{
				throw ex;
			}
		}

		public int method_1(int int_0, int int_1, int int_2, int int_3)
		{
			int num = int_1 * int_3 / int_2;
			return int_0 + ((num > int_1) ? int_1 : num);
		}

		private void method_2(GClass19.GClass22 gclass22_0)
		{
			if (gclass22_0.Collections.hashTableLose.Count > 0)
			{
				foreach (DictionaryEntry dictionaryEntry in gclass22_0.Collections.hashTable)
				{
					GClass0.smethod_2(gclass22_0.Collections.hashTableFailSecond, dictionaryEntry.Key.ToString(), dictionaryEntry.Value.ToString());
				}
			}
			if (gclass22_0.Collections.hashTableFailSecond.Count > 0)
			{
				List<GClass29> list = GClass28.smethod_2();
				int num = 1;
				foreach (DictionaryEntry dictionaryEntry in gclass22_0.Collections.hashTableFailSecond)
				{
					list.Add(new GClass29
					{
						FailId = num,
						FailUrl = dictionaryEntry.Key.ToString(),
						FailPath = dictionaryEntry.Value.ToString()
					});
					num++;
				}
			}
			if (gclass22_0.Collections.log.Count > 0)
			{
				List<GClass30> list2 = GClass28.smethod_3();
				int num = 1;
				foreach (GClass19.GClass21 current in gclass22_0.Collections.log)
				{
					list2.Add(new GClass30
					{
						LogId = num,
						LogCategoryId = (int)current.Category,
						LogMessage = current.Message
					});
					num++;
				}
			}
			if (gclass22_0.Collections.hashTableNoExists.Count > 0)
			{
				List<GClass31> list3 = GClass28.smethod_4();
				int num = 1;
				foreach (DictionaryEntry dictionaryEntry in gclass22_0.Collections.hashTableNoExists)
				{
					list3.Add(new GClass31
					{
						NoExistsId = num,
						NoExistsUrl = dictionaryEntry.Key.ToString(),
						NoExistsPath = dictionaryEntry.Value.ToString()
					});
					num++;
				}
			}
		}

		private void method_3(GClass19.GClass22 gclass22_0)
		{
			if (gclass22_0._Lists.Count > 0)
			{
				List<int> list = new List<int>();
				for (int i = 0; i < gclass22_0._Lists.Count; i++)
				{
					int arg = this.method_1(0, 4, gclass22_0._Lists.Count, i);
					gclass22_0.ReportMessageProgressAction(gclass22_0, "检测编码:" + gclass22_0._Lists[i]._Url, arg, GClass19.GClass25.GEnum1.Message);
					if (!string.IsNullOrEmpty(gclass22_0._Lists[i]._PageCodeString))
					{
						gclass22_0._Lists[i]._PageCode = Encoding.GetEncoding(gclass22_0._Lists[i]._PageCodeString);
					}
					else
					{
						string text = GClass38.smethod_0(gclass22_0._Lists[i]._Url, gclass22_0.UserAgent);
						if (!string.IsNullOrEmpty(text))
						{
							gclass22_0._Lists[i]._PageCodeString = text;
							gclass22_0._Lists[i]._PageCode = Encoding.GetEncoding(text);
						}
						else
						{
							GClass27 gClass = GClass28.smethod_1();
							gclass22_0._Lists[i]._PageCodeString = gClass.Encoding;
							gclass22_0._Lists[i]._PageCode = Encoding.GetEncoding(gClass.Encoding);
							gclass22_0.Collections.log.Add(new GClass19.GClass21
							{
								Category = GClass19.GClass21.GEnum0.EncodingFail,
								Message = gclass22_0._Lists[i]._Url
							});
						}
					}
				}
				if (list.Count > 0)
				{
					foreach (int current in list)
					{
						gclass22_0._Lists.RemoveAt(current);
					}
				}
			}
			if (gclass22_0._Lists.Count > 0)
			{
				for (int i = 0; i < gclass22_0._Lists.Count; i++)
				{
					int arg = this.method_1(4, 15, gclass22_0._Lists.Count, i);
					gclass22_0.ReportMessageProgressAction(gclass22_0, "下载页面:" + gclass22_0._Lists[i]._Url, arg, GClass19.GClass25.GEnum1.Message);
					gclass22_0._Lists[i]._PageContent = GClass39.smethod_1(gclass22_0._Lists[i]._Url, gclass22_0._Lists[i]._PageCode, gclass22_0.UserAgent, gclass22_0._Lists[i]._Cookies, gclass22_0._Lists[i]._CookieContainer);
					System.Text.RegularExpressions.MatchCollection matchCollection = new System.Text.RegularExpressions.Regex(GClass2.GClass15.GClass18.baseTag, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline).Matches(gclass22_0._Lists[i]._PageContent);
					if (matchCollection.Count > 0)
					{
						string input = GClass36.smethod_0(matchCollection[0].Value);
						System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(GClass2.GClass15.GClass18.baseTagHref, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
						System.Text.RegularExpressions.MatchCollection matchCollection2 = regex.Matches(input);
						if (matchCollection2.Count > 0)
						{
							gclass22_0._Lists[i]._BaseHref = matchCollection2[0].Value;
						}
					}
				}
			}
			if (gclass22_0._Lists.Count > 0)
			{
				gclass22_0.ReportMessageProgress(gclass22_0, "创建文件夹...", 19);
				this.method_4(gclass22_0);
			}
			if (gclass22_0._Lists.Count > 0)
			{
				for (int i = 0; i < gclass22_0._Lists.Count; i++)
				{
					int arg = this.method_1(20, 10, gclass22_0._Lists.Count, i);
					gclass22_0.ReportMessageProgressAction(gclass22_0, gclass22_0._Lists[i]._Url, arg, GClass19.GClass25.GEnum1.Message);
					string string_ = (!string.IsNullOrEmpty(gclass22_0._Lists[i]._BaseHref)) ? gclass22_0._Lists[i]._BaseHref : gclass22_0._Lists[i]._Url;
					gclass22_0._Lists[i]._PageContent = GClass43.smethod_1(gclass22_0, gclass22_0._Lists[i]._PageContent, string_, gclass22_0._Lists[i]._HtmlBackground);
					gclass22_0._Lists[i]._PageContent = GClass43.smethod_5(gclass22_0, gclass22_0._Lists[i]._PageContent, string_, gclass22_0._Lists[i]._HtmlPicture);
					gclass22_0._Lists[i]._PageContent = GClass43.smethod_3(gclass22_0, gclass22_0._Lists[i]._PageContent, string_, gclass22_0._Lists[i]._HtmlFlash);
					gclass22_0._Lists[i]._PageContent = GClass43.smethod_4(gclass22_0, gclass22_0._Lists[i]._PageContent, string_, gclass22_0._Lists[i]._HtmlLink, gclass22_0._Lists[i]._PageCode);
					gclass22_0._Lists[i]._PageContent = GClass43.smethod_6(gclass22_0, gclass22_0._Lists[i]._PageContent, string_, gclass22_0._Lists[i]._HtmlScript);
				}
			}
			Hashtable hashtable = new Hashtable();
			foreach (DictionaryEntry dictionaryEntry in gclass22_0.Collections.hashTable)
			{
				if (!File.Exists(dictionaryEntry.Value.ToString()))
				{
					hashtable.Add(dictionaryEntry.Key, dictionaryEntry.Value);
				}
			}
			gclass22_0.Collections.hashTable = hashtable;
			gclass22_0.Collections.hashTableLose = gclass22_0.Collections.hashTable;
			gclass22_0.Collections.DownloadNumberTotal = gclass22_0.Collections.hashTable.Count;
			if (gclass22_0.Collections.hashTable.Count <= 0)
			{
				for (int i = 30; i <= 95; i += 5)
				{
					gclass22_0.ReportMessageProgress(gclass22_0, "跳过下载文件……", i);
					Thread.Sleep(150);
				}
			}
			else
			{
				this.method_7(gclass22_0, 30, 65);
			}
			if (gclass22_0._Lists.Count > 0)
			{
				for (int i = 0; i < gclass22_0._Lists.Count; i++)
				{
					gclass22_0.ReportMessage(gclass22_0, "生成Html：" + gclass22_0._Lists[i]._Url);
					gclass22_0._Lists[i]._PageContent = new System.Text.RegularExpressions.Regex(GClass2.GClass15.GClass18.baseTag, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline).Replace(gclass22_0._Lists[i]._PageContent, "");
					GClass42.smethod_1(gclass22_0._rootWebSite + gclass22_0._Lists[i]._FilePath + gclass22_0._Lists[i]._FileName, gclass22_0._Lists[i]._PageContent, gclass22_0._Lists[i]._PageCode, true);
				}
				gclass22_0.ReportMessage(gclass22_0, "Html生成完毕，正在处理失败文件…");
			}
			Thread.Sleep(50);
			if (gclass22_0.Collections.hashTableFailFirst.Count > 0)
			{
				gclass22_0.ReportMessage(gclass22_0, "开始下载失败文件！");
				this.method_9(gclass22_0, 96, 4);
			}
		}

		private void method_4(GClass19.GClass22 gclass22_0)
		{
			foreach (GClass19.GClass22.GClass23 current in gclass22_0._Lists)
			{
				GClass42.smethod_0(gclass22_0._rootWebSite + current._FilePath);
			}
			GClass42.smethod_0(gclass22_0._rootWebSite + gclass22_0._DirectoryFlash);
			GClass42.smethod_0(gclass22_0._rootWebSite + gclass22_0._DirectoryCss);
			GClass42.smethod_0(gclass22_0._rootWebSite + gclass22_0._DirectoryJs);
			GClass42.smethod_0(gclass22_0._rootWebSite + gclass22_0._DirectoryPicture);
			GClass42.smethod_0(gclass22_0._rootWebSite + gclass22_0._DirectoryBackground);
			GClass42.smethod_0(gclass22_0._rootWebSite + gclass22_0._DirectoryFonts);
		}

		private void method_5(GClass19.GClass22 gclass22_0, string string_0)
		{
			if (Directory.Exists(string_0) && Directory.GetDirectories(string_0).Length == 0 && Directory.GetFiles(string_0).Length == 0)
			{
				try
				{
					Directory.Delete(string_0);
				}
				catch (Exception ex)
				{
					gclass22_0.Collections.log.Add(new GClass19.GClass21
					{
						Category = GClass19.GClass21.GEnum0.FailedDeleteFolder,
						Message = ex.Message.ToString()
					});
				}
			}
		}

		private void method_6(Hashtable hashtable_0, string string_0)
		{
			if (hashtable_0.Count > 0)
			{
				lock (hashtable_0.SyncRoot)
				{
					foreach (DictionaryEntry dictionaryEntry in hashtable_0)
					{
						if (dictionaryEntry.Key.ToString().ToLower().Trim() == string_0.ToLower().Trim())
						{
							hashtable_0.Remove(dictionaryEntry.Key);
							break;
						}
					}
				}
			}
		}

		private void method_7(GClass19.GClass22 gclass22_0, int int_0, int int_1)
		{
			//IAsyncResult asyncResult = null;
			//int count = gclass22_0.Collections.hashTable.Count;
			//using (IDictionaryEnumerator enumerator = gclass22_0.Collections.hashTable.GetEnumerator())
			//{
			//	while (enumerator.MoveNext())
			//	{
			//		DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
			//		GClass49 gClass = new GClass49();
			//		gClass.Url = dictionaryEntry.Key.ToString();
			//		gClass.Path = dictionaryEntry.Value.ToString().Replace("\\", "/");
			//		gClass.ProgressBarCurrentValue = int_0;
			//		gClass.ProgressBarAddValue = int_1;
			//		gClass.TotalNumber = count;
			//		gClass.ReportMessageProgress = gclass22_0.ReportMessageProgress;
			//		gClass.obj = gclass22_0;
			//		GClass40 @object = new GClass40();
			//		GDelegate0 gDelegate = new GDelegate0(@object.method_0);
			//		asyncResult = gDelegate.BeginInvoke(dictionaryEntry.Key.ToString(), dictionaryEntry.Value.ToString(), 5000, gclass22_0.UserAgent, new AsyncCallback(this.method_8), gClass);
			//	}
			//	goto IL_119;
			//}
			//IL_112:
			//Thread.Sleep(50);
			//IL_119:
			//if (asyncResult.IsCompleted)
			//{
			//	return;
			//}
			//goto IL_112;
		}

		private void method_8(IAsyncResult iasyncResult_0)
		{
			AsyncResult asyncResult = (AsyncResult)iasyncResult_0;
			GClass49 gClass = (GClass49)asyncResult.AsyncState;
			GDelegate0 gDelegate = (GDelegate0)asyncResult.AsyncDelegate;
			bool flag = gDelegate.EndInvoke(iasyncResult_0);
			gClass.obj.Collections.DownloadNumberFirst++;
			string str = "";
			if (!flag)
			{
				if (!GClass39.smethod_0(gClass.Url, gClass.obj.UserAgent))
				{
					GClass0.smethod_2(gClass.obj.Collections.hashTableNoExists, gClass.Url, gClass.Path);
					str = "[跳过下载]";
				}
				else
				{
					GClass0.smethod_2(gClass.obj.Collections.hashTableFailFirst, gClass.Url, gClass.Path);
					str = "[下载失败]";
				}
			}
			gClass.ReportMessageProgress(gClass.obj, str + gClass.Url, this.method_1(gClass.ProgressBarCurrentValue, gClass.ProgressBarAddValue, gClass.TotalNumber, gClass.obj.Collections.DownloadNumberFirst));
			this.method_6(gClass.obj.Collections.hashTableLose, gClass.Url);
		}

		private void method_9(GClass19.GClass22 gclass22_0, int int_0, int int_1)
		{
			//IAsyncResult asyncResult = null;
			//using (IDictionaryEnumerator enumerator = gclass22_0.Collections.hashTableFailFirst.GetEnumerator())
			//{
			//	while (enumerator.MoveNext())
			//	{
			//		DictionaryEntry dictionaryEntry = (DictionaryEntry)enumerator.Current;
			//		GClass49 gClass = new GClass49();
			//		gClass.Url = dictionaryEntry.Key.ToString();
			//		gClass.Path = dictionaryEntry.Value.ToString();
			//		gClass.ProgressBarCurrentValue = int_0;
			//		gClass.ProgressBarAddValue = int_1;
			//		gClass.TotalNumber = gclass22_0.Collections.hashTableFailFirst.Count;
			//		gClass.ReportMessageProgress = gclass22_0.ReportMessageProgress;
			//		gClass.obj = gclass22_0;
			//		GClass40 @object = new GClass40();
			//		GDelegate0 gDelegate = new GDelegate0(@object.method_0);
			//		asyncResult = gDelegate.BeginInvoke(dictionaryEntry.Key.ToString(), dictionaryEntry.Value.ToString(), gclass22_0._Timeout, gclass22_0.UserAgent, new AsyncCallback(this.method_10), gClass);
			//	}
			//	goto IL_100;
			//}
			//IL_F9:
			//Thread.Sleep(50);
			//IL_100:
			//if (asyncResult.IsCompleted)
			//{
			//	return;
			//}
			//goto IL_F9;
		}

		private void method_10(IAsyncResult iasyncResult_0)
		{
			AsyncResult asyncResult = (AsyncResult)iasyncResult_0;
			GClass49 gClass = (GClass49)asyncResult.AsyncState;
			GDelegate0 gDelegate = (GDelegate0)asyncResult.AsyncDelegate;
			bool flag = gDelegate.EndInvoke(iasyncResult_0);
			gClass.obj.Collections.DownloadNumberSecond++;
			gClass.ReportMessageProgress(gClass.obj, "重新下载" + (flag ? "(成功)" : "(失败)") + ":" + gClass.Url, this.method_1(gClass.ProgressBarCurrentValue, gClass.ProgressBarAddValue, gClass.TotalNumber, gClass.obj.Collections.DownloadNumberSecond));
			if (!flag)
			{
				GClass0.smethod_2(gClass.obj.Collections.hashTableFailSecond, gClass.Url, gClass.Path);
			}
			this.method_6(gClass.obj.Collections.hashTableLose, gClass.Url);
		}
	}
}
